package com.huaruan.youdi.views.fragments;


import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.huaruan.youdi.R;
import com.huaruan.youdi.eventbus.EventBus;
import com.huaruan.youdi.eventbus.EventNames;
import com.huaruan.youdi.http.okhttp.Constant;
import com.huaruan.youdi.http.okhttp.HttpCallback;
import com.huaruan.youdi.http.okhttp.HttpUtils;
import com.huaruan.youdi.models.IncomeModel;
import com.huaruan.youdi.utils.AppUtils;
import com.huaruan.youdi.utils.UserUtils;
import com.huaruan.youdi.views.MainTabViewPager;
import com.huaruan.youdi.views.widget.SlidingTabLayout;

import java.util.ArrayList;

import butterknife.BindView;
import pl.droidsonroids.gif.GifImageView;

public class MainFragment extends BaseFragment {
    @BindView(R.id.iv_gif)
    GifImageView iv_gif;
    @BindView(R.id.tv_open)
    TextView tv_open;
    @BindView(R.id.tv_price)
    TextView tv_price;
    @BindView(R.id.tv_point)
    TextView tv_point;
    @BindView(R.id.tv_num)
    TextView tv_num;
    @BindView(R.id.tabLayout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.viewPager)
    MainTabViewPager viewPager;
    @BindView(R.id.iv_refresh)
    ImageView iv_refresh;

    String[] tabs = new String[]{"待配送", "已送达"};
    private ArrayList<BaseFragment> fragments = new ArrayList<BaseFragment>();
    PagerAdapter adapter;
    IncomeModel model;

    @Override
    public int IGetContentViewResId() {
        return R.layout.fragment_main;
    }


    @Override
    public void IFindViews(final View view) {
        tv_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (model != null) {
                    openOrder();
                }
            }
        });

        fragments.clear();
        for (int i = 0; i < tabs.length; i++) {
            if ("待配送".equals(tabs[i])) {
                MainChildFragment fragment = new MainChildFragment();
                Bundle bundle = new Bundle();
                bundle.putString("type", "4");
                fragment.setArguments(bundle);
                fragments.add(fragment);
            } else if ("已送达".equals(tabs[i])) {
                MainChildFragment fragment = new MainChildFragment();
                Bundle bundle = new Bundle();
                bundle.putString("type", "6");
                fragment.setArguments(bundle);
                fragments.add(fragment);
            }
        }

        FragmentManager fragmentManager = getChildFragmentManager();
        adapter = new PagerAdapter(fragmentManager);
        viewPager.setAdapter(adapter);
        tabLayout.setViewPager(viewPager);
        viewPager.setOffscreenPageLimit(3);

        iv_refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getInstance().notifiDataUpdate(EventNames.REFRESH_MAIN, "");
            }
        });
    }

    private void openOrder() {
        String url = Constant.OPEN_ORDER + UserUtils.getUid();
        new HttpUtils.Builder(IGetContext())
                .url(url)
                .callback(new HttpCallback() {
                    @Override
                    public void success(Object object) {
                        if ("1".equals(model.isOpen)) {
                            model.isOpen = "0";
                            iv_gif.setVisibility(View.GONE);
                            AppUtils.toastShort(IGetContext(), "关闭派单");
                        } else {
                            model.isOpen = "1";
                            iv_gif.setVisibility(View.VISIBLE);
                            AppUtils.toastShort(IGetContext(), "开启派单");
                        }
                    }
                }).start();
    }

    private void income() {
        String url = Constant.GET_INCOME + UserUtils.getUid();
        new HttpUtils.Builder(IGetContext())
                .url(url)
                .modelClass(IncomeModel.class)
                .callback(new HttpCallback() {
                    @Override
                    public void success(Object object) {
                        model = (IncomeModel) object;
                        initHead();
                    }
                }).start();
    }

    private void initHead() {
        if (model != null) {
            tv_price.setText(model.income);
            tv_point.setText(model.grade);
            tv_num.setText(model.todayOrder);
            if ("1".equals(model.isOpen)) {
                iv_gif.setVisibility(View.VISIBLE);
            } else {
                iv_gif.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void IInitData() {
    }

    @Override
    public void IRequest() {
        income();
    }

    class PagerAdapter extends FragmentStatePagerAdapter {


        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public BaseFragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (tabs.length - 1 >= position)
                return tabs[position];
            return "未定义";
        }

    }


}
